﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Kitware.VTK;


namespace HAVTKPack
{
   public  class ShowRiverFilter:GeometryFilter
   {

       #region  构造函数

       public ShowRiverFilter(string id,int scaleSize,System .Drawing .Color color,vtkGlyphSource2DDrawType drawType)
           : base(id)
       {

           this._glyphScaleSize = scaleSize;
           this._glyphColor = color;
           this._drawType = drawType;
       
       }
       //public ShowRiverFilter(string id,  List<int[]> ijks)
       //    : base(id)
       //{


       //}


       #endregion


       #region  字段属性



       int _glyphScaleSize = 10;//类型设置为 int？？


       System.Drawing.Color _glyphColor = System.Drawing.Color.Red;

       vtkGlyphSource2DDrawType _drawType = vtkGlyphSource2DDrawType.TriangleUp ;



       List<int[]> ijks = new List<int[]>();
       /// <summary>
       /// 标记哪个单元格需要添加River标志
       /// </summary>
       public List<int[]> Ijks
       {
           get { return ijks; }
           set
           {

               ijks = value;


           }
       }



       #endregion


       #region VTK流水线所需要的对象

       vtkUnstructuredGrid _unStructuredGridData;

       vtkConnectivityFilter _con;

       vtkGeometryFilter _geo;

       vtkPolyData _polyData;


       vtkGlyphSource2D gs;

       vtkTransformCoordinateSystems xform;
       vtkGlyph2D glypher;
       vtkPolyDataMapper2D mapper;
       vtkActor2D glyphActor;



       #endregion



       public override void Render()
       {
           try
           {


               _unStructuredGridData = (vtkUnstructuredGrid)this.ParentNode.Geometry.OutPutData;


               if (this._unStructuredGridData == null)
               {
                   return;
               }
               _con = vtkConnectivityFilter.New();
               _con.SetInput(_unStructuredGridData);

               _geo = vtkGeometryFilter.New();
               _geo.SetInput(_con.GetOutput());

               _polyData = vtkPolyData.New();
               _polyData = _geo.GetOutput();



               this.OutputPolyData = _geo.GetOutput();
               this.OutputUnStructuredGridData = _unStructuredGridData;



               #region 尝试添加十字标志

               //vtkSphereSource sphere = vtkSphereSource.New();
               //sphere.SetPhiResolution(10);
               //sphere.SetThetaResolution(20);

               xform = vtkTransformCoordinateSystems.New();

               //需要将数据进行坐标系转换，否则标志的默认位置在坐标原点开始排列。

               xform.SetInput(_unStructuredGridData);
               //xform.SetInputConnection(sphere.GetOutputPort());
               //xform.SetInput(_polyData);
               //xform.SetInput(model);

               xform.SetInputCoordinateSystemToWorld();
               //xform.SetInputCoordinateSystemToViewport();
               //xform.SetInputCoordinateSystemToDisplay ();

               xform.SetOutputCoordinateSystemToDisplay();
               //xform.SetOutputCoordinateSystemToViewport ();
               //xform.SetOutputCoordinateSystemToWorld ();


               xform.SetViewport(this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer());



               gs = vtkGlyphSource2D.New();

               this.SetGlyphSourceDrawType(this._drawType);
               gs.SetScale(this._glyphScaleSize);

               //gs.SetGlyphTypeToCross ();
               ////gs.SetScale(1);
               //gs.SetScale(5);
               gs.FilledOn ();
               gs.CrossOff();


               #region  使用vtkGlyph2D添加符号

               // Create a table of glyphs

               glypher = vtkGlyph2D.New();

               glypher.SetInputConnection(xform.GetOutputPort());
               //glypher.SetInput(_unStructuredGridData);
               //glypher.SetInput(_polyData);
               //glypher.SetInput(model);
               glypher.SetSource(gs.GetOutput());


               glypher.SetScaleModeToDataScalingOff();


               mapper = vtkPolyDataMapper2D.New();

               mapper.SetInputConnection(glypher.GetOutputPort());
               //mapper.SetInputConnection(gl3d.GetOutputPort());


               glyphActor = vtkActor2D.New();
               glyphActor.SetMapper(mapper);

               if (this._glyphColor != null)
               {
                   glyphActor.GetProperty().SetColor(_glyphColor.R / 255.0, _glyphColor.G / 255.0, _glyphColor.B / 255.0);
               }
               else
               {
                   glyphActor.GetProperty().SetColor(1, 0, 0);
               }

               if (this._visiability)
               {
                   glyphActor.VisibilityOn();
               }
               else
               {
                   glyphActor.VisibilityOff();
               }

               this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(glyphActor);

               #endregion


               #region 未使用

               // gl3d = vtkGlyph3D.New();

               //gl3d.SetInput(_unStructuredGridData);
               ////gl3d.SetInput(_polyData);
               //gl3d.SetSource(gs.GetOutput());

               //gl3d.SetScaleModeToDataScalingOff();//关闭缩放？？？

               ////gl3d.SetVectorModeToUseNormal();

               ////gl3d.SetScaleModeToScaleByScalar();



               // pMapper = vtkPolyDataMapper.New();
               //pMapper.SetInputConnection(gl3d.GetOutputPort());



               // glActor = vtkActor.New();
               //glActor.SetMapper(pMapper);
               //glActor.GetProperty().SetColor(1, 0, 0);
               //glActor.VisibilityOn();


               //this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(glActor);



               #endregion


               #endregion

               #region 未使用


               //_polyDataMapper = vtkPolyDataMapper.New();
               //_polyDataMapper.SetInput(_polyData);   
               //_polyDataMapper.ScalarVisibilityOff();

               //_actor = vtkActor.New();
               //_actor.SetMapper(_polyDataMapper);
               //_actor.GetProperty().SetColor(1, 0, 0);//红色

               //_actor.VisibilityOn();
               //_actor.GetProperty().SetRepresentationToWireframe();

               //this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(_actor);

               #endregion



           }
           catch (Exception)
           {

               System.Windows.Forms.MessageBox.Show("ShowRiverFilter渲染过程出错！");
               return;
           }



           //throw new NotImplementedException();
       }

       public override void StopRender()
       {


           //资源的销毁

           #region VTK流水线的销毁

           if (this.glyphActor != null)
           {
               this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().RemoveActor(glyphActor);
               this.glyphActor.Dispose();
           }
           //if (this.glActor !=null )
           //{
           //    this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().RemoveActor(glActor);
           //    this.glActor.Dispose();
           //}
           //if (this._actor != null)
           //{
           //    this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().RemoveActor(_actor);
           //    this._actor.Dispose();
           //}

           if (this.mapper != null)
           {
               this.mapper.Dispose();
           }
           //if (this.pMapper!=null )
           //{
           //    this.pMapper.Dispose();
           //}

           if (this.glypher != null)
           {
               this.glypher.Dispose();
           }
           //if (this.gl3d!=null )
           //{
           //    this.gl3d.Dispose();
           //}
           if (this.gs != null)
           {
               this.gs.Dispose();
           }
           if (this.xform != null)
           {
               this.xform.Dispose();
           }

           if (this._polyData != null)
           {
               this._polyData.Dispose();
           }
           if (this._geo != null)
           {
               this._geo.Dispose();
           }
           if (this._con != null)
           {
               this._con.Dispose();
           }
           if (this._unStructuredGridData != null)
           {
               this._unStructuredGridData.Dispose();
           }
           //if (this.lut !=null )
           //{
           //    this.lut.Dispose();
           //}

           if (this.ParentNode.Geometry.OutPutData != null)
           {
               this.ParentNode.Geometry.OutPutData.Dispose();
           }


           #endregion


           //throw new NotImplementedException();
       }

       #region 属性改变方法



       /// <summary>
       /// 重写可见性属性
       /// </summary>
       protected override void OnSetVisibility()
       {
           base.OnSetVisibility();

           if (this.ParentNode.Scene == null)
           {
               return;

           }

           if (this.ParentNode.Scene.IsRendering)
           {
               if (this._visiability)
               {
                   this.glyphActor.VisibilityOn();
                   //this.glActor.VisibilityOn();
                   //this._actor.VisibilityOn();
               }
               else
               {
                   this.glyphActor.VisibilityOff();
                   //this.glActor.VisibilityOff();
                   //this._actor.VisibilityOff();
               }

               //this.ParentNode.Scene.Update();
           }
       }


       protected override void OnSetRepresentationType()
       {
           base.OnSetRepresentationType();


           if (this.ParentNode.Scene == null)
           {
               return;

           }

           if (this.ParentNode.Scene.IsRendering)
           {
               if (this._repType == REPRESENTATIONTYPE.POINT)
               {
                   //this.glyphActor.GetProperty().SetRepresentationToPoints();
                   //this.glActor.GetProperty().SetRepresentationToPoints();
                   //this._actor.GetProperty().SetRepresentationToPoints();
               }
               else if (this._repType == REPRESENTATIONTYPE.SURFACE)
               {
                   //this.glyphActor.GetProperty().SetRepresentationToSurface();
                   //this.glActor.GetProperty().SetRepresentationToSurface();
                   //this._actor.GetProperty().SetRepresentationToSurface();
               }
               else
               {
                   //this.glyphActor.GetProperty().SetRepresentationToWireframe();
                   //this.glActor.GetProperty().SetRepresentationToWireframe();
                   //this._actor.GetProperty().SetRepresentationToWireframe();
               }


               //this.ParentNode.Scene.Update();
           }

       }



       #endregion



       #region 公有方法

       public void SetVTKGlyphSource2DData(bool visible, int size, System.Drawing.Color color, vtkGlyphSource2DDrawType drawType)
       {
           if (this.gs != null)
           {

               //判断数据是否有效 如果无效则指定值或不做改变
               gs.CrossOff();
               //gs.FilledOff();
               gs.FilledOn();
               //gs.SetColor(color.R, color.G, color.B);


               if (this.glyphActor!=null )
               {
                   this.glyphActor.GetProperty().SetColor(color .R/255.0,color .G/255.0,color .B/255.0);
               }


               if (size > 0)
               {
                   gs.SetScale(size);
                   //gs.SetScale2();
               }

               this.SetGlyphSourceDrawType(drawType);


               if (this.glyphActor!=null )
               {
                    this.Visiability = visible;
               }


               this._glyphScaleSize = size;

               this._glyphColor = color;

               this._drawType = drawType;


           }
           else
           {
               this.Visiability = visible;

               this._glyphScaleSize = size;

               this._glyphColor = color;

               this._drawType = drawType;

           }



       }


       #endregion
       #region 内部辅助方法


       /// <summary>
       /// 设置glyphSource的图案类型
       /// </summary>
       /// <param name="drawType"></param>
       private void SetGlyphSourceDrawType(vtkGlyphSource2DDrawType drawType)
       {
           if (gs == null)
           {
               return;
           }
           switch (drawType)
           {
               case vtkGlyphSource2DDrawType.Square:

                   gs.SetGlyphTypeToSquare();

                   break;
               case vtkGlyphSource2DDrawType.Circle:

                   gs.SetGlyphTypeToCircle();


                   break;
               case vtkGlyphSource2DDrawType.Diamond:

                   gs.SetGlyphTypeToDiamond();

                   break;
               case vtkGlyphSource2DDrawType.TriangleUp:

                   gs.SetGlyphTypeToTriangle();

                   break;
               case vtkGlyphSource2DDrawType.Line:

                   //gs.SetGlyphTypeToDash();

                   break;
               case vtkGlyphSource2DDrawType.DottedLine:



                   break;
               default:

                   gs.SetGlyphTypeToSquare();

                   break;
           }



       }


       #endregion


   }
}
